package principal;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class ListePluviometresFactory {

	public ListePluviometresFactory() {
		listePluvios = null;
	}

	public static ListePluviometres getListesPluviometres(String radar,
			Date date) {
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.setTime(date);
		int jour = calendar.get(Calendar.DAY_OF_MONTH);
		int heure = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		GregorianCalendar calendarTmp = new GregorianCalendar();
		calendarTmp.setTime(calendar.getTime());
		calendarTmp.set(Calendar.HOUR_OF_DAY, 6);
		calendarTmp.set(Calendar.MINUTE, 0);
		Date dateDebut;
		Date dateFin;
		if (heure < 6 || (heure == 6 && minute == 0)) {
			dateFin = calendarTmp.getTime();
			calendarTmp.set(Calendar.DAY_OF_MONTH, jour - 1);
			dateDebut = calendarTmp.getTime();
		} else {
			dateDebut = calendarTmp.getTime();
			calendarTmp.set(Calendar.DAY_OF_MONTH, jour + 1);
			dateFin = calendarTmp.getTime();
		}
		if (listePluvios == null) {
			listePluvios = new ListePluviometres(radar, dateDebut, dateFin);
		} else {
			if (!listePluvios.getRadar().equals(radar)
					|| !listePluvios.getDateDebut().equals(dateDebut)
					|| !listePluvios.getDateFin().equals(dateFin)) {
				listePluvios = new ListePluviometres(radar, dateDebut, dateFin);
			}
		}
		return listePluvios;
	}

	private static ListePluviometres listePluvios;
}
